package com.fulu.game.bi.redis.enums;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.EnumSet;

/**
 * redis key 前缀
 */
public enum RedisKeyEnum {

    /**
     * bi - 验证码输入次数
     */
    BI_CAPTCHA_TIMES,
    /**
     * 时间间隔
     */
    BI_TIME_INTERVAL_KEY,
    /**
     * 用户token
     */
    BI_PLAY_TOKEN,
    ;


    private String prefix;

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    @Component
    public static class EnumConfig {

        /**  获取环境定义的前缀 */
        @Value("${config.evn.prefix}")
        private String prefix;

        @PostConstruct
        public void postConstruct() {
            for (RedisKeyEnum anEnum: EnumSet.allOf(RedisKeyEnum.class)){
               anEnum.setPrefix(this.prefix);
            }
        }
    }


    private static final String SERVER_NAME = "game_bi";
    private static final String SPLIT = "-";
    private static final String SPLIT2 = ":";



    public String generateKey(int id) {
        return prefix + SPLIT + this.name() + SPLIT + id;
    }

    public String generateKey(int parentId, int id) {
        return prefix + SPLIT + this.name() + SPLIT + parentId + SPLIT + id;
    }

    public String generateKey(String id) {
        return prefix + SPLIT + this.name() + SPLIT + id;
    }

    public String generateKey(String parentId, String id) {
        return prefix + SPLIT + this.name() + SPLIT + parentId + SPLIT + id;
    }

    public String generateKey(String parentId, Integer id) {
        return prefix + SPLIT + this.name() + SPLIT + parentId + SPLIT + id;
    }

    public String generateKey() {
        return prefix + SPLIT + this.name();
    }

    public String generateDirKey(int id) {
        return prefix + SPLIT + this.name() + SPLIT2 + id;
    }

    public String generateDirKey(String dir) {
        return prefix + SPLIT + this.name() + SPLIT2 + dir;
    }

    public String generateDirKey(String dir, String key) {
        return prefix + SPLIT + this.name() + SPLIT2 + dir + SPLIT2 + key;
    }

    public String generateCommonActivityDirKey(String bizKey, String dir) {
        return prefix + SPLIT + bizKey + SPLIT2 + dir;
    }


    /**
     * 获取渠道统计相关key
     *
     * @return
     */
    public String getPlatformStatKey(String bizKey, int platform, int os, String day) {
        return prefix + SPLIT + this.name() + SPLIT + bizKey + SPLIT + platform + SPLIT + os + SPLIT + day;
    }

    /**
     * 获取渠道统计相关key
     *
     * @return
     */
    public String getPlatformStatKey(String bizKey, int platform, int os, String day, int statTimeEnum) {
        return prefix + SPLIT + this.name() + SPLIT + bizKey + SPLIT + platform + SPLIT + os + SPLIT + day + SPLIT + statTimeEnum;
    }

    /**
     * 获取渠道统计相关key
     *
     * @return
     */
    public String getPlatformStatKey(String bizKey, int platform, int os, String begin, String end) {
        return prefix + SPLIT + this.name() + SPLIT + bizKey + SPLIT + platform + SPLIT + os + SPLIT + begin + SPLIT + end;
    }

    /**
     * 获取渠道统计相关key
     *
     * @return
     */
    public String getPlatformStatKey(String bizKey, int platform, int os, String begin, String end, int statTimeEnum,Integer labelId) {
        return prefix + SPLIT + this.name() + SPLIT + bizKey + SPLIT + platform + SPLIT + os + SPLIT + begin + SPLIT + end + SPLIT + statTimeEnum + SPLIT + labelId;
    }

    /**
     * 获取邀新活动统计相关key
     *
     * @return
     */
    public String  getInviteStatKey(String bizKey, Integer activityId, String begin, String end, int statTimeEnum) {
        return prefix + SPLIT + this.name() + SPLIT + bizKey + SPLIT + activityId + SPLIT + begin + SPLIT + end + SPLIT + statTimeEnum;
    }

    /**
     * @return
     */
    public String getFaqKey(Integer userId, Integer faqId, String date) {
        return prefix + SPLIT + this.name() + SPLIT + userId + SPLIT + faqId + SPLIT + date;
    }

    /**
     * @return
     */
    public String getUserDrawKey(String cashNo) {
        return prefix + SPLIT + "userDrawRedisKey" + this.name() + SPLIT + cashNo;
    }

    /**
     * @return
     */
    public String getGuildDrawKey(String cashNo) {
        return prefix + SPLIT + "guildDrawRedisKey" + this.name() + SPLIT + cashNo;
    }


}
